﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊处方记录
    /// </summary>
    public class OtpRxRecService : OutpatientAppService, IOtpRxRecService
    {
        private IRepository<OtpRxRec, Guid> _otpRxRecRepository;
        private OtpRxManger _otpRxManger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="otpRxRecRepository"></param>
        public OtpRxRecService(IRepository<OtpRxRec, Guid> otpRxRecRepository, OtpRxManger otpRxManger)
        {
            _otpRxRecRepository = otpRxRecRepository;
            _otpRxManger = otpRxManger;
        }

        /// <summary>
        /// 获取处方记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpRxRecDto>> GetListAsync(OtpRxRecInput input)
        {
            if (input.Stas == 1) //已审核未交费
            {
                var list = await _otpRxManger.GetOtpRxPayList(input.OtpNo);
                var itemsDto = ObjectMapper.Map<List<OtpRxRec>, List<OtpRxRecDto>>(list);

                return new PagedResultDto<OtpRxRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var iQuery = await _otpRxRecRepository.GetQueryableAsync();
                iQuery = iQuery.WhereIf(input.OtpMdtrtRecId != null, o => input.OtpMdtrtRecId == input.OtpMdtrtRecId);

                if (input.Pi == 0)
                {
                    var itemsDto = ObjectMapper.Map<List<OtpRxRec>, List<OtpRxRecDto>>(iQuery.ToList());

                    return new PagedResultDto<OtpRxRecDto>(itemsDto.Count, itemsDto);
                }
                else
                {
                    var totalCount = iQuery.Count();
                    var items = iQuery.Page(input.Pi, input.Ps);
                    var itemsDto = ObjectMapper.Map<List<OtpRxRec>, List<OtpRxRecDto>>(items.ToList());

                    return new PagedResultDto<OtpRxRecDto>(totalCount, itemsDto);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OtpRxRecDto> GetAsync(Guid id)
        {
            var otpRxRec = await _otpRxRecRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<OtpRxRec, OtpRxRecDto>(otpRxRec);

            return itemDto;
        }

        /// <summary>
        /// 提交处方记录
        /// </summary>
        /// <returns></returns>
        public async Task<OtpRxRecDto> PostAsync(OtpRxRecCreateDto input)
        { 
            var otpRxRex = ObjectMapper.Map<OtpRxRecCreateDto, OtpRxRec>(input);
            otpRxRex = await _otpRxRecRepository.InsertAsync(otpRxRex);
            var itemDto = ObjectMapper.Map<OtpRxRec, OtpRxRecDto>(otpRxRex);

            return itemDto;
        }

        /// <summary>
        /// 门诊处方记录审核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OtpRxRecDto> PostChkAsync(Guid id)
        {
            var otpRxRec = await _otpRxRecRepository.GetAsync(id);
            var chkDrCodg = CurrentUser.UserName;
            var chkDrName = CurrentUser.Name;
            otpRxRec.SetChk(chkDrCodg, chkDrName);
            var itemDto = ObjectMapper.Map<OtpRxRec, OtpRxRecDto>(otpRxRec);

            return itemDto;
        }
    }
}
